home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / PaintRef.java < prev    next >
Text File  |  1998-06-30  |  6KB  |  182 lines

  1. /*
  2.  * @(#)PaintRef.java    1.6  98/01/30
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not disclose
  8.  * such Confidential Information and shall use it only in accordance with the
  9.  * terms of the license agreement you entered into with Sun.
  10.  * 
  11.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  12.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  14.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  15.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  16.  * ITS DERIVATIVES.
  17.  * 
  18.  */
  19. package com.sun.java.swing;
  20.  
  21. import java.io.File;
  22. import java.awt.*;
  23. import java.awt.image.*;
  24.  
  25. /**
  26.  * PaintRef that can be used for filling a region.  Can be either a
  27.  * color or an image.
  28.  * @author James Gosling
  29.  */
  30. public class PaintRef implements ImageObserver {
  31.     private Color clr;
  32.     private Image img;
  33.     private int imWidth, imHeight;
  34.  
  35.     public PaintRef () {
  36.     }
  37.     public PaintRef (Color c) {
  38.     setColor(c);
  39.     }
  40.     public PaintRef (Image i) {
  41.     setTile(i);
  42.     }
  43.     public String toString() {
  44.     String s = clr != null ? clr.toString() : null;
  45.     if (img != null)
  46.         if (s != null)
  47.         s = s + "," + img;
  48.         else
  49.         s = img.toString();
  50.     return "PaintRef[" + s + "]";
  51.     }
  52.     public void setTile(Image tile) {
  53.     if (tile == null)
  54.         return;
  55.     img = tile;
  56.     imWidth = img.getWidth(this);
  57.     imHeight = img.getHeight(this);
  58.     }
  59.     public Image getTile() { return img; }
  60.     public void setColor(Color c) {
  61.     if (c == null)
  62.         return;
  63.     clr = c;
  64.     }
  65.     public boolean imageUpdate(Image img,
  66.                    int infoflags,
  67.                    int x, int y,
  68.                    int width, int height) {
  69.     if ((infoflags & (ERROR | ABORT)) != 0) {
  70.         img = null;
  71.         return false;
  72.     }
  73.     if ((infoflags & WIDTH) != 0)
  74.         imWidth = img.getWidth(this);
  75.     if ((infoflags & HEIGHT) != 0)
  76.         imHeight = img.getHeight(this);
  77.     return (infoflags & ALLBITS) == 0;
  78.     }
  79.     public Color getColor() {
  80.     return clr;
  81.     }
  82.     public void fillRect(Graphics g, int X, int Y, int W, int H, ImageObserver o) {
  83.     if (img != null
  84.         && imWidth > 0
  85.         && imHeight > 0) {
  86.         int x0 = X / imWidth * imWidth;
  87.         int xlim = X + W;
  88.         int ylim = Y + H;
  89.         for (int y = Y / imHeight * imHeight; y < ylim; y += imHeight)
  90.         for (int x = x0; x < xlim; x += imWidth)
  91.             g.drawImage(img,
  92.                 x, y,
  93.                 imWidth, imHeight,
  94.                 o);
  95.     } else {
  96.         Color c = g.getColor();
  97.         g.setColor(clr == null ? SystemColor.window : clr);
  98.         g.fillRect(X, Y, W, H);
  99.         g.setColor(c);
  100.     }
  101.     }
  102.     public void fillRect(Graphics g, Rectangle r, ImageObserver o) {
  103.     fillRect(g, r.x, r.y, r.width, r.height, o);
  104.     }
  105.     public void fill(Graphics g, ImageObserver o) {
  106.     fillRect(g, g.getClipBounds(), o);
  107.     }
  108.     /** Finds a named color from the defined SystemColors */
  109.     public static PaintRef findNamed(String s) {
  110.     for (int i = ls.length; --i >= 0;)
  111.         if (s.equalsIgnoreCase(ls[i])) {
  112.         if (lp == null)
  113.             lp = new PaintRef[SystemColor.NUM_COLORS];
  114.         PaintRef r = lp[i];
  115.         if (r == null) lp[i] = r = new PaintRef(cl[i]);
  116.         return r;
  117.         }
  118.     return null;
  119.     }
  120.  
  121.     private static PaintRef[] lp;
  122.     private static String[] ls = new String[SystemColor.NUM_COLORS];
  123.     static {
  124.     ls[SystemColor.DESKTOP] = "desktop";
  125.     ls[SystemColor.ACTIVE_CAPTION] = "active_caption";
  126.     ls[SystemColor.ACTIVE_CAPTION_TEXT] = "active_caption_text";
  127.     ls[SystemColor.ACTIVE_CAPTION_BORDER] = "active_caption_border";
  128.     ls[SystemColor.INACTIVE_CAPTION] = "inactive_caption";
  129.     ls[SystemColor.INACTIVE_CAPTION_TEXT] = "inactive_caption_text";
  130.     ls[SystemColor.INACTIVE_CAPTION_BORDER] = "inactive_caption_border";
  131.     ls[SystemColor.WINDOW] = "window";
  132.     ls[SystemColor.WINDOW_BORDER] = "window_border";
  133.     ls[SystemColor.WINDOW_TEXT] = "window_text";
  134.     ls[SystemColor.MENU] = "menu";
  135.     ls[SystemColor.MENU_TEXT] = "menu_text";
  136.     ls[SystemColor.TEXT] = "text";
  137.     ls[SystemColor.TEXT_TEXT] = "text_text";
  138.     ls[SystemColor.TEXT_HIGHLIGHT] = "text_highlight";
  139.     ls[SystemColor.TEXT_HIGHLIGHT_TEXT] = "text_highlight_text";
  140.     ls[SystemColor.TEXT_INACTIVE_TEXT] = "text_inactive_text";
  141.     ls[SystemColor.CONTROL] = "control";
  142.     ls[SystemColor.CONTROL_TEXT] = "control_text";
  143.     ls[SystemColor.CONTROL_HIGHLIGHT] = "control_highlight";
  144.     ls[SystemColor.CONTROL_LT_HIGHLIGHT] = "control_lt_highlight";
  145.     ls[SystemColor.CONTROL_SHADOW] = "control_shadow";
  146.     ls[SystemColor.CONTROL_DK_SHADOW] = "control_dk_shadow";
  147.     ls[SystemColor.SCROLLBAR] = "scrollbar";
  148.     ls[SystemColor.INFO] = "info";
  149.     ls[SystemColor.INFO_TEXT] = "info_text";
  150.     }
  151.  
  152.     private static Color[] cl = new Color[SystemColor.NUM_COLORS];
  153.     static {
  154.     cl[SystemColor.DESKTOP] = SystemColor.desktop;
  155.     cl[SystemColor.ACTIVE_CAPTION] = SystemColor.activeCaption;
  156.     cl[SystemColor.ACTIVE_CAPTION_TEXT] = SystemColor.activeCaptionText;
  157.     cl[SystemColor.ACTIVE_CAPTION_BORDER] = SystemColor.activeCaptionBorder;
  158.     cl[SystemColor.INACTIVE_CAPTION] = SystemColor.inactiveCaption;
  159.     cl[SystemColor.INACTIVE_CAPTION_TEXT] = SystemColor.inactiveCaptionText;
  160.     cl[SystemColor.INACTIVE_CAPTION_BORDER] = SystemColor.inactiveCaptionBorder;
  161.     cl[SystemColor.WINDOW] = SystemColor.window;
  162.     cl[SystemColor.WINDOW_BORDER] = SystemColor.windowBorder;
  163.     cl[SystemColor.WINDOW_TEXT] = SystemColor.windowText;
  164.     cl[SystemColor.MENU] = SystemColor.menu;
  165.     cl[SystemColor.MENU_TEXT] = SystemColor.menuText;
  166.     cl[SystemColor.TEXT] = SystemColor.text;
  167.     cl[SystemColor.TEXT_TEXT] = SystemColor.textText;
  168.     cl[SystemColor.TEXT_HIGHLIGHT] = SystemColor.textHighlight;
  169.     cl[SystemColor.TEXT_HIGHLIGHT_TEXT] = SystemColor.textHighlightText;
  170.     cl[SystemColor.TEXT_INACTIVE_TEXT] = SystemColor.textInactiveText;
  171.     cl[SystemColor.CONTROL] = SystemColor.control;
  172.     cl[SystemColor.CONTROL_TEXT] = SystemColor.controlText;
  173.     cl[SystemColor.CONTROL_HIGHLIGHT] = SystemColor.controlHighlight;
  174.     cl[SystemColor.CONTROL_LT_HIGHLIGHT] = SystemColor.controlLtHighlight;
  175.     cl[SystemColor.CONTROL_SHADOW] = SystemColor.controlShadow;
  176.     cl[SystemColor.CONTROL_DK_SHADOW] = SystemColor.controlDkShadow;
  177.     cl[SystemColor.SCROLLBAR] = SystemColor.scrollbar;
  178.     cl[SystemColor.INFO] = SystemColor.info;
  179.     cl[SystemColor.INFO_TEXT] = SystemColor.infoText;
  180.     }
  181. }
  182.